<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="业务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskId">
              <SelectPlanTaskInput
                :disabled="formDisabled"
                :projectId="projectId"
                :redistributionDtList="redistributionDtList"
                :isRefresh="true"
                v-model="model.taskId"
                @back="changeTaskId"
                placeholder="请选择业务名称"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="生产团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productTeam">
              <select-team-input disabled placeholder="请选择生产团队" v-model="model.productTeam" :tdlx="2" :triggerChange="true"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="款项分配占比(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentRate">
              <a-input-number
                :disabled="formDisabled"
                :max="100"
                :min="0.01"
                :precision="2"
                v-model="model.paymentRate"
                @blur="paymentRateBlur"
                placeholder="请输入款项占比"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="款项分配金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentMoney">
              <a-input-number
                disabled
                :max="999999999"
                :min="0"
                :precision="2"
                v-model="model.paymentMoney"
                placeholder="请输入款项分配金额"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="任务说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taskDesc">
              <a-input disabled v-model="model.taskDesc" :max-length="36" placeholder="请输入任务说明"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import SelectPlanTaskInput from './SelectPlanTaskInput'
  import moment from 'moment'
  import { mapGetters } from 'vuex'
  import Decimal from 'decimal.js'
  export default {
    name: 'ErpOmcTaskRedistributionPlanTaskForm',
    components: {
      SelectUserByDep,
      SelectTeamInput,
      SelectCompanyInput,
      SelectCustomerInput,
      SelectPlanTaskInput
    },
    props: {
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        redistributionDtList: [], // 分配的任务信息列表
        projectId: '',
        planId: '',
        totalPrice: '', // 款项金额
        flag: false,
        model: {
          projectId: '',
          planId: ''
        },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
          taskId: [
            { required: true, message: '请选择业务名称!', trigger: 'change' }
          ],
          productTeam: [
            { required: true, message: '请选择生产团队!', trigger: 'change' }
          ],
          paymentRate: [
            { required: true, message: '请输入款项分配占比!' }
          ],
          paymentMoney: [
            { required: true, message: '请输入款项分配金额!' }
          ]
        },
        url: {
          add: '/api/erp/omc/contractmanager/erpOmcContractPlanTask/add',
          edit: '/api/erp/omc/contractmanager/erpOmcContractPlanTask/edit',
          queryById: '/api/erp/omc/contractmanager/erpOmcContractPlanTask/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      ...mapGetters(['userInfo']),
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs['form'].clearValidate();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      // 计算款项分配金额
      paymentRateBlur (val) {
          if (this.model.paymentRate === null || this.model.paymentRate === '' || this.model.paymentRate === undefined) {
            this.$set(this.model, 'paymentMoney', '')
          } else {
            let totalPrice = this.totalPrice || 0
            let paymentRate = this.model.paymentRate
            let amount = totalPrice * paymentRate / 100
            let paymentMoney = Math.round((amount + Number.EPSILON) * 100) / 100;
            this.$set(this.model, 'paymentMoney', paymentMoney)
          }
      },
      changeTaskId (val) {
        let data = val[0]
        this.$set(this.model, 'productTeam', data.productTeam); // 生产团队
        this.$set(this.model, 'taskDesc', data.taskDescription); // 任务说明
        this.$set(this.model, 'categoryName', data.categoryName); // 业务名称
        this.$set(this.model, 'productTeamName', data.productTeam_dictText); // 业务名称
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            this.model.projectId = this.projectId
            this.model.planId = this.planId
            that.$emit('ok', this.model, this.flag);
          }
        })
      }
    }
  }
</script>
